package com.yonyou.crm.common.security.util;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpEncryptUtil {

	private final static Logger logger = LoggerFactory.getLogger(HttpEncryptUtil.class);
	
	/**
	 * 将request请求的参数进行des解密
	 * @param paramStr 传入参数值
	 * @return
	 * @throws Exception
	 */
	public static String desDecodeRequest(String paramStr) throws Exception{
		
		if (StringUtils.isNotBlank(paramStr)) {
			IEncryption ien = Base64DesEncrypt.getInstance();
			byte[] desParam = ien.decode(paramStr.getBytes());
			paramStr = new String(desParam);
		}
		
		return paramStr;
	}
	
	/**
	 * 将返回结果进行DES加密
	 * @param data 成功结果数据
	 * @return
	 */
	public static String desEncodeData(Object data){
		JSONObject dataJson = JSONObject.fromObject(data);
		try {
			IEncryption ien = Base64DesEncrypt.getInstance();
			byte[] dataByteArr = ien.encode(dataJson.toString().getBytes());
			String desData = new String(dataByteArr);
			return desData;
		} catch (Exception e) {
			logger.error("this is error json!","exception log", e);
		}
		return null;
	}
	
}
